package com.ossean;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Component;

import com.ossean.dao.DBDest;
import com.ossean.dao.DBSource;
import com.ossean.dao.GatherDao;
import com.ossean.dao2.PlatformProjectDao;

@Component
public class GetSynonymsProcess {

    Logger logger = Logger.getLogger(this.getClass());
	@Resource
	private DBSource dbSource;
	@Resource
	public DBDest dbDest;
	@Resource
	private GatherDao gatherDao;
	@Resource
	private PlatformProjectDao paltformDao;
	//创建进程池，一次20个进程
   private ExecutorService pool = Executors.newFixedThreadPool(20);
	
	private static String gatherProjectsTableName =TableName.gatherProjectsTableName;
	private static String synonymsTableName = TableName.synonymsTableName;
	private static String pointerTableName = TableName.pointerTableName;
	private static int batchSize = 10000;
	
	public void start() throws InterruptedException
	{
		int lastId = gatherDao.selectLastId(gatherProjectsTableName); 
		int maxId = gatherDao.getMaxId(gatherProjectsTableName);
		Thread.sleep(1000);
	
		int begin = lastId;
		int end;
		while(begin < maxId)
		{
			end = begin + batchSize -1;
			if(end < maxId)
			{
				GetSynonymsThread getSynonymsThread = (GetSynonymsThread)AppContext.appContext.getBean("GetSynonymsThread");
				getSynonymsThread.setBorder(begin, end);
				getSynonymsThread.setSourceTable(gatherProjectsTableName);
				//getSynonymsThread.run();
				pool.execute(getSynonymsThread);
				begin = begin + batchSize;
			}
			else
			{
				end = maxId;
				GetSynonymsThread getSynonymsThread = (GetSynonymsThread)AppContext.appContext.getBean("GetSynonymsThread");
				getSynonymsThread.setBorder(begin, end);
				getSynonymsThread.setSourceTable(gatherProjectsTableName);
				pool.execute(getSynonymsThread);
				//getSynonymsThread.run();
				break;
			}
		}
		
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"classpath:/applicationContext*.xml");
		GetSynonymsProcess Main = applicationContext.getBean(GetSynonymsProcess.class);
		Main.start();
	}
}
